﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using MySql.Data.MySqlClient;
using Models;
using Common;

namespace DAL
{
    /// <summary>
    /// 门型业务处理
    /// </summary>
    public class MenxingDAL
    {
        //展示所有
        public DataTable ListAll(Dictionary<string,string> dic) {
            string sql = string.Empty;
            if(!string.IsNullOrWhiteSpace(dic["mxbh"])){
                sql += " and mxbh like '%" + dic["mxbh"] + "%'";
            }
            if (!string.IsNullOrWhiteSpace(dic["mxsm"]))
            {
                sql += " and mxsm like '%" + dic["mxsm"] + "%'";
            }
            DataTable dt = SqlHelper.ExecuteDataTable("select * from mxbh where isdel=@isdel "+sql +" order by edittime desc", new MySqlParameter("@isdel","0"));
            return dt;

        }
        //保存数据
        public int SaveData(Menxing mx) {
            int result = -1;
            result = SqlHelper.ExecuteNonQuery("insert into mxbh(mxbh,mxsm,kxbh,xbmc,price,glass,special,isdel,addTime,editTime,addUser,editUser,specialname,addprice,issoft,limitcc,limitkuan,limitgao) values(@mxbh,@mxsm,@kxbh,@xbmc,@price,@glass,@special,@isdel,@addTime,@editTime,@addUser,@editUser,@specialname,@addprice,@issoft,@limitcc,@limitkuan,@limitgao)",
                new MySqlParameter("@mxbh",mx.mxbh),new MySqlParameter("@mxsm",mx.mxsm),new MySqlParameter("@kxbh",mx.kxbh),new MySqlParameter("@xbmc",mx.xbmc),
                new MySqlParameter("@price", mx.price), new MySqlParameter("@glass", mx.glass), new MySqlParameter("@special", mx.IsSpecial), new MySqlParameter("@isdel", "0"),
                new MySqlParameter("@addTime", DateTime.Now.ToString()), new MySqlParameter("@editTime", DateTime.Now.ToString()), new MySqlParameter("@addUser", RemeberName.Username), 
                new MySqlParameter("@editUser", RemeberName.Username),new MySqlParameter("@specialname", mx.SpecialName), new MySqlParameter("@addprice", mx.AddPrice),
                new MySqlParameter("@issoft", mx.IsRb), new MySqlParameter("@limitcc", mx.LimitCC), new MySqlParameter("@limitkuan", mx.LimitKuan), new MySqlParameter("@limitgao", mx.LimitGao));

            return result;
        }
        //更新数据
        public int UpdateData(Menxing mx) {
            int result = -1;
            result = SqlHelper.ExecuteNonQuery("update mxbh set mxbh=@mxbh,mxsm=@mxsm,kxbh=@kxbh,xbmc=@xbmc,price=@price,glass=@glass ,editTime=@editTime,editUser=@editUser,special=@special,specialname=@specialname,addprice=@addprice,issoft=@issoft,limitcc=@limitcc,limitkuan=@limitkuan,limitgao=@limitgao  where id =@id",
                 new MySqlParameter("@mxbh", mx.mxbh), new MySqlParameter("@mxsm", mx.mxsm), new MySqlParameter("@kxbh", mx.kxbh), new MySqlParameter("@xbmc", mx.xbmc),
                new MySqlParameter("@price", mx.price), new MySqlParameter("@glass", mx.glass),
                new MySqlParameter("@editTime", DateTime.Now.ToString()), new MySqlParameter("@editUser", RemeberName.Username),
                new MySqlParameter("@special", mx.IsSpecial), new MySqlParameter("@specialname", mx.SpecialName), new MySqlParameter("@addprice", mx.AddPrice),
                new MySqlParameter("@issoft", mx.IsRb), new MySqlParameter("@limitcc", mx.LimitCC), new MySqlParameter("@limitkuan", mx.LimitKuan), new MySqlParameter("@limitgao", mx.LimitGao), new MySqlParameter("@id", mx.id));
            return result;
        }
        //假删除
        public int DelData(string id) {
            return SqlHelper.ExecuteNonQuery("update mxbh set isdel=@isdel,editTime=@editTime,editUser=@editUser where id =@id",
                new MySqlParameter("@isdel", 1), new MySqlParameter("@editTime", DateTime.Now.ToString()), new MySqlParameter("@editUser", RemeberName.Username), new MySqlParameter("@id", id));
        }
        //根据编号查询
        public Menxing GetById(string id) {
            DataTable dt = SqlHelper.ExecuteDataTable("select * from mxbh where id = @id",new MySqlParameter("@id",id));
            if (dt.Rows.Count <= 0)
            {
                return null;
            }
            else if (dt.Rows.Count > 1)
            {
                throw new Exception("有重复值");
            }
            else {
                return ToMenxing(dt.Rows[0]);
            }
        }
        public Menxing ToMenxing(DataRow dr) {
            Menxing mx = new Menxing();
            mx.id=Convert.ToInt32(dr["id"]);
            mx.kxbh = dr["kxbh"].ToString();
            mx.mxsm = dr["mxsm"].ToString();
            mx.mxbh = dr["mxbh"].ToString();
            mx.xbmc =dr["xbmc"].ToString();
            mx.glass = Convert.ToInt32(dr["glass"]);
            mx.price = dr["price"].ToString();
            mx.IsSpecial = Convert.ToInt32(dr["special"]);
            mx.SpecialName = dr["specialname"].ToString();
            mx.AddPrice = dr["addprice"].ToString();
            mx.IsRb = Convert.ToInt32(dr["issoft"]);
            mx.LimitCC = Convert.ToInt32(dr["limitcc"]);
            mx.LimitGao = dr["limitgao"].ToString();
            mx.LimitKuan = dr["limitkuan"].ToString();
            return mx;
        }
    }
}
